package com.google.code.sms.command;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.google.code.sms.controller.MediaFolderSettingsController;
import com.google.code.sms.domain.MediaFolder;
import org.apache.commons.lang.StringUtils;

/**
 * Command used in {@link MediaFolderSettingsController}.
 *
 * @author Sindre Mehus
 */
public class MediaFolderSettingsCommand {

    private String interval;
    private String hour;
    private boolean scanning;
    private boolean organizeByFolderStructure;
    private List<MediaFolderInfo> mediaFolders;
    private MediaFolderInfo newMediaFolder;
    private boolean reload;

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public boolean isScanning() {
        return scanning;
    }

    public void setScanning(boolean scanning) {
        this.scanning = scanning;
    }

    public List<MediaFolderInfo> getMediaFolders() {
        return mediaFolders;
    }

    public void setMediaFolders(List<MediaFolderInfo> mediaFolders) {
        this.mediaFolders = mediaFolders;
    }

    public MediaFolderInfo getNewMediaFolder() {
        return newMediaFolder;
    }

    public void setNewMediaFolder(MediaFolderInfo newMediaFolder) {
        this.newMediaFolder = newMediaFolder;
    }

    public void setReload(boolean reload) {
        this.reload = reload;
    }

    public boolean isReload() {
        return reload;
    }

    public boolean isOrganizeByFolderStructure() {
        return organizeByFolderStructure;
    }

    public void setOrganizeByFolderStructure(boolean organizeByFolderStructure) {
        this.organizeByFolderStructure = organizeByFolderStructure;
    }

    public static class MediaFolderInfo {

        private Integer id;
        private String path;
        private String name;
        private boolean enabled;
        private boolean delete;
        private boolean existing;

        public MediaFolderInfo(MediaFolder mediaFolder) {
            id = mediaFolder.getId();
            path = mediaFolder.getPath();
            name = mediaFolder.getName();
            enabled = mediaFolder.isEnabled();
            existing = mediaFolder.getFile().exists() && mediaFolder.getFile().isDirectory();
        }

        public MediaFolderInfo() {
            enabled = true;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public boolean isDelete() {
            return delete;
        }

        public void setDelete(boolean delete) {
            this.delete = delete;
        }

        public MediaFolder toMediaFolder() {
            String path = StringUtils.trimToNull(this.path);
            if (path == null) {
                return null;
            }
            File file = new File(path);
            String name = StringUtils.trimToNull(this.name);
            if (name == null) {
                name = file.getName();
            }
            return new MediaFolder(id, path, name, enabled, new Date());
        }

        public boolean isExisting() {
            return existing;
        }
    }
}